package com.github.pig.admin.service.park.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pig.admin.mapper.park.EmployeeInfoMapper;
import com.github.pig.admin.model.park.EmployeeInfo;
import com.github.pig.admin.model.park.UserAppDto;
import com.github.pig.admin.service.park.IEmployeeInfoService;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.DateTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sxf
 * @since 2018-12-17
 */
@Service
public class EmployeeInfoServiceImpl extends ServiceImpl<EmployeeInfoMapper, EmployeeInfo> implements IEmployeeInfoService {
    @Autowired
    private EmployeeInfoMapper employeeMapper;

    @Override
    public UserAppDto selectComByUserName(String loginName) {

        List<UserAppDto> list = employeeMapper.selectComByUserName(loginName,CommonConstant.COMMUNITY_DEFAULT_FLAG);
        if(list!=null && !list.isEmpty()){
            return list.get(0);
        }else{
            List<UserAppDto> listAll = employeeMapper.selectComByUserName(loginName,CommonConstant.COMMUNITY_NOT_DEFAULT_FLAG);
            if(listAll!=null && !listAll.isEmpty()) {
                employeeMapper.updateDefaultCompany(list.get(0).getCompanyId(),list.get(0).getUsername(),CommonConstant.COMMUNITY_DEFAULT_FLAG);
                return list.get(0);
            }
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDefaultCompany(Integer companyId, String username) {
        /**
         * 先取消全部默认 再设置当前默认
         */
        employeeMapper.updateDefaultCompany(null,username, CommonConstant.COMMUNITY_NOT_DEFAULT_FLAG);
        employeeMapper.updateDefaultCompany(companyId,username,CommonConstant.COMMUNITY_DEFAULT_FLAG);
    }

    @Override
    public boolean judgeFinger(Integer companyId, String userName) {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("company_id",companyId);
        wrapper.eq("phone",userName);
        List<EmployeeInfo> list = this.selectList(wrapper);
        if(list!=null && !list.isEmpty()){
            if(list.get(0).getFingerTime().before(new Date())){
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Date d = DateTools.parseDate("2019-02-22 12:22:22",DateTools.FULLTIME_FORMAT);
        Date d1 = new Date();
        System.out.println(d.before(d1));
    }
}
